home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / src / joystick.lha / joy.c next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  2.5 KB  |  104 lines

  1. /* joy.c                                                             */
  2. /* Written in September 1993 by George F. McBay (gfm@gnu.ai.mit.edu) */
  3. /* This code is 100% Public Domain.                                  */
  4. #include <exec/types.h>
  5. #include <hardware/custom.h>
  6. #include <hardware/cia.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10. #define UP     1        /* Define values for possible joystick events */
  11. #define DOWN   2
  12. #define LEFT   4
  13. #define RIGHT  8
  14. #define FIRE1 16
  15. #define FIRE2 32
  16.  
  17. #define POTGOR *(UWORD *)0xDFF016     
  18. struct CIA *cia = (struct CIA *) 0xBFE001;
  19.  
  20. void main(int argc, char *argv[]);
  21. UBYTE ReadJoystick(UWORD joynum);
  22.  
  23. void main(int argc, char *argv[])
  24. {
  25.  UBYTE joyval;
  26.  UWORD count = 0, maxcount, joynum;
  27.     
  28.     if(argc == 0)
  29.         exit(0); /* Must be run from a Shell */
  30.  
  31.     if(argc != 3) {
  32.         printf("Usage: %s [joyport] [number of events to process]\n", argv[0]);
  33.           exit(0);
  34.     }
  35.  
  36.     if((atoi(argv[1]) != 0) && (atoi(argv[1]) != 1)) {
  37.         printf("Error: The joyport parameter must be equal to 0 or 1.\n");
  38.         exit(0);
  39.     }
  40.  
  41.     joynum = atoi(argv[1]);
  42.     maxcount = atoi(argv[2]);
  43.         
  44.     do { 
  45.           joyval = ReadJoystick(joynum); /* Example program reads joyport number 1, the */
  46.                                          /* function can also read joyport 0 */
  47.  
  48.           if(joyval & UP)
  49.             printf("Up ");
  50.  
  51.           if(joyval & DOWN)
  52.             printf("Down ");
  53.  
  54.           if(joyval & LEFT)
  55.             printf("Left ");
  56.     
  57.           if(joyval & RIGHT)
  58.             printf("Right ");
  59.  
  60.           if(joyval  & FIRE1)
  61.             printf("(Fire-1) ");
  62.  
  63.           if(joyval & FIRE2)
  64.         printf("(Fire-2) ");
  65.  
  66.           if(joyval != 0) {
  67.         printf("\n");
  68.         count++;
  69.           }
  70.  
  71.     }    while(count <  maxcount); /* Get 'maxcount' Joystick Events */
  72.       
  73.  }
  74.  
  75. UBYTE ReadJoystick(UWORD joynum)
  76. {
  77.  extern struct Custom far custom;
  78.  UBYTE ret = 0;
  79.  UWORD joy;
  80.     
  81.       if(joynum == 0)                /* JoyPort 0? */
  82.           joy = custom.joy0dat;  /* If so, use joyport 0 */
  83.       else
  84.               joy = custom.joy1dat;  /* Otherwise, default to joyport 1 */
  85.  
  86.       ret += (joy >> 1 ^ joy) & 0x0100 ? UP : 0;  
  87.       ret += (joy >> 1 ^ joy) & 0x0001 ? DOWN : 0;
  88.  
  89.       ret += joy & 0x0200 ? LEFT : 0;
  90.       ret += joy & 0x0002 ? RIGHT : 0;
  91.     
  92.       if(joynum == 0) {
  93.         ret += !(cia->ciapra & 0x0040) ? FIRE1 : 0;  /* Read FireButtons */
  94.         ret += !(POTGOR & 0x0400) ? FIRE2 : 0;          /* on joyport 0     */
  95.       }
  96.       else {
  97.           ret += !(cia->ciapra & 0x0080) ? FIRE1 : 0; /* Read FireButtons */
  98.           ret += !(POTGOR & 0x4000) ? FIRE2 : 0;         /* on joyport 1     */
  99.       }
  100.  
  101.       return(ret);
  102. }
  103.  
  104.